package com.fun_ordering.service.impl;

import com.fun_ordering.mapper.*;
import com.fun_ordering.pojo.*;
import com.fun_ordering.service.MaterialStockService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.MaterialStockExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 原材料表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class MaterialStockServiceImpl implements MaterialStockService {

	@Autowired
	private MaterialStockMapper materialStockMapper;
	@Autowired
	private FoLossMapper foLossMapper;


	@Override
	//    统计每日损耗的食材用量并进行原料减少
	public List<MaterialStock> lossMater() {
		//材料库存表
		List<MaterialStock> materialStockList = materialStockMapper.selectByExample(null);
		//损耗表
		List<FoLoss> foLossList = foLossMapper.selectByExample(null);
		for (int i = 0; i < foLossList.size(); i++) {
			//损耗表的每日损耗量
			FoLoss foLoss = foLossList.get(i);
			BigDecimal loNumber = foLoss.getLoNumber();
			//总库存量
			MaterialStock materialStock = materialStockList.get(i);
			BigDecimal totalNumber = materialStock.getStockNumber();
			// 计算更新后的库存量
			BigDecimal updatedStockNumber = totalNumber.subtract(loNumber);
			if (foLoss.getMaterialName().equals(materialStock.getStockName())){
				// 更新材料库存对象的库存量
				materialStock.setStockNumber(updatedStockNumber);
				materialStockMapper.updateByPrimaryKey(materialStock);
				break;
			}
		}
		return materialStockMapper.selectByExample(null);
	}

	/**
	 * 查询全部
	 */
	@Override
	public List<MaterialStock> findAll() {
		return materialStockMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<MaterialStock> page=   (Page<MaterialStock>) materialStockMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(MaterialStock materialStock) {
		materialStockMapper.insert(materialStock);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(MaterialStock materialStock){
		materialStockMapper.updateByPrimaryKey(materialStock);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param stockId
	 * @return
	 */
	@Override
	public MaterialStock findOne(Integer stockId){
		return materialStockMapper.selectByPrimaryKey(stockId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] stockIds) {
		for(Integer stockId:stockIds){
			materialStockMapper.deleteByPrimaryKey(stockId);
		}		
	}
	
	
		@Override
	public PageResult findPage(MaterialStock materialStock, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		MaterialStockExample example=new MaterialStockExample();
		Criteria criteria = example.createCriteria();
		
		if(materialStock!=null){			
						if(materialStock.getStockName()!=null && materialStock.getStockName().length()>0){
				criteria.andStockNameLike("%"+materialStock.getStockName()+"%");
			}	
		}
		
		Page<MaterialStock> page= (Page<MaterialStock>)materialStockMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

//	/**
//	 *作用：根据订单原料出库
//	 *方法名：
//	 *参数: [foOrder]
//	 *返回值: void
//	 */
//	@Override
//	public boolean enterMaterialStock(FoOrder foOrder) {
//		//获取订单id
//		Integer orderId = foOrder.getOrderId();
//		//通过订单id获取订单菜品详细集合
//		/--------------------------------------------------------------------------
//		List<OrderDetail> dishlist =  orderDetailMapper.selectByOrderId(orderId);
//		for (OrderDetail detail : dishlist) {
//			//获取单个菜品的id
//			Integer dishId = detail.getDishId();
//			//获取单个菜品的数量
//			BigDecimal dishNumber = detail.getDishNumber();
//			//根据菜品id获取所用菜品原料集合
//			-------------------------------------------------------------------------
//			List<MaterialStock> stocklist = materialStockMapper.selectByDishId(dishId);
//			for (MaterialStock materialStock : stocklist) {
//				//获取单个菜品的单个原料id
//				Integer stockId = materialStock.getStockId();
//				//制作单个菜品的所需单个原料的数量
//				BigDecimal stockNumber = materialStock.getStockNumber();
//				//菜品数量乘以单个菜品的所需单个原料的数量
//				BigDecimal dishStockSum = dishNumber.multiply(stockNumber);
//				//调用原材料库存表里的通过id查询原料库存的方法
//				//-----------------------------------------------------------------------
//				MaterialStock stock = materialStockMapper.selectByPrimaryKey(stockId);
//				BigDecimal dbNumber = stock.getStockNumber();
//				//compareTo()>0,第一个对象大于第二个对象
//				//compareTo()<0,第一个对象小于第二个对象
//				if (dishStockSum.compareTo(dbNumber)>0){
//					return false;
//				}else {
//					//调用原材料库存表里的修改方法，通过id修改原料库存
//					//------------------------------------------------------------
//					materialStockMapper.updateStockNumberByStockId(stockId,dishStockSum);
//					return true;
//				}
//			}
//		}
//	}


}
